package com.ryder.oss.util;

import com.ryder.oss.entity.FileInfo;
import io.minio.*;
import io.minio.errors.MinioException;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

/**
 * 描述: minio 文件操作工具
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0 // 版本号，指明从哪个版本开始
 * 更新日期: 2025/02/07
 */

@Component
public class MinioUtil {

    @Autowired
    private MinioClient minioClient;

    /**
     * 创建 bucket容器
     */
    public void createBucket(String bucket) throws Exception {
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!exists) {
            // 创建bucket 如果不存在
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }

    /**
     * 上传文件
     */
    public void uploadFile(InputStream inputStream, String bucket, String objectName) throws Exception {
        minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
                .stream(inputStream, -1, 5242889L).build());
    }

    /**
     * 列出所有桶
     */
    public List<String> getALlBucket() throws Exception {
        if (minioClient == null) {
            throw new IllegalStateException("MinioClient has not been initialized.");
        }
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).toList();
    }

    /**
     * 列出所有桶及文件
     */
    public List<FileInfo> getALlFile(String bucket) throws Exception {
        Iterable<Result<Item>> results = minioClient
                .listObjects(ListObjectsArgs.builder()
                        .bucket(bucket).build());
        List<FileInfo> fileInfoList = new LinkedList<>();
        for (Result<Item> result : results) {
            FileInfo fileInfo = new FileInfo();
            Item item = result.get();
            fileInfo.setFileName(item.objectName());
            fileInfo.setDirectoryFlag(item.isDir());
            fileInfo.setEtag(item.etag());
            fileInfoList.add(fileInfo);
        }
        return fileInfoList;
    }

    /**
     * 下载文件
     */
    public InputStream downLoad(String bucket, String fileName) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder()
                .bucket(bucket).object(fileName).build());
    }

    /**
     * 删除桶
     */
    public void deleteBucket(String bucket) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
    }

    /**
     * 删除文件
     */
    public void deleteObject(String bucket, String objectName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder()
                .bucket(bucket).object(objectName).build());
    }

    public String getPreviewUrl(String bucket, String objectName) throws Exception {
        return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                .bucket(bucket).object(objectName).build());
    }
}
